/**
 * Created by Administrator on 2018/11/20 0020.
 */
import Vue from 'vue';
// const host = process.env.API_ROOT;
const host = "https://www.trianglefit.top/triangle";
function request (url, method, data, header = {}) {
  wx.showLoading({
    title: '加载中' // 数据请求前loading
  });

  //拼装请求头
  let token = wx.getStorageSync("token");
  let userInfo = wx.getStorageSync("userInfo");
  let AuthorizationStr ="";
  if(token){
    AuthorizationStr = "TOKEN_" + token;
  }
  if(userInfo && userInfo != undefined && userInfo.id){
    if(AuthorizationStr){
      AuthorizationStr += ","
    }
    AuthorizationStr += "userId_" + userInfo.id;
  }


  return new Promise((resolve, reject) => {
    wx.request({
      // url: Vue.prototype.$baseURL + url,   //具体的接口地址
      url: host + url,   //具体的接口地址
      method: method,
      data: data,
      header: {
        'content-type': 'application/json',
        'Authorization' : AuthorizationStr
      },
      success: function (res) {
        //判断当前用户是否被拉黑，如果被拉黑调回登录页面
        if(res.data.code == -1){
          wx.showModal({
            title: '提示',
            showCancel:false,
            content: '您已被拉黑，请联系管理员并重新登录',
            success (res) {
              if (res.confirm) {
                wx.reLaunch({
                  url: '../login/main?type=blacklist'
                })
              }
            }
          })
        }
        wx.hideLoading();
        resolve(res.data);
      },
      fail: function (res) {
        wx.hideLoading();
        resolve(res.data);
      },
      complete: function (res) {
        wx.hideLoading();
        resolve(res.data);
      }
    })
  })
}

function get (obj) {
  return request(obj.url, 'GET', obj.data)
}

function post (obj) {
  return request(obj.url, 'POST', obj.data)
}

export default {
  request,
  get,
  post
}
